let customHead = require('../../common/customHead.js')
Page({
    behaviors: [customHead],
    data: {
        showWhiteBar: false,
        orderSumInfo: null,
        // dontShowTipModal: true, //不显示强制完善手机号提示框
    },
    onLoad: function(options) {
        let Authorization = wx.getStorageSync('Authorization')
        if (Authorization) {
            //获取用户信息
            this.getUserInfo(() => {
                this.data.userInfo.is_auth === 0 && this.getUserProfile() //没有授权过微信昵称头像时获取
            })
        }
    },
    onShow() {
        let Authorization = wx.getStorageSync('Authorization')
        if (Authorization) this.getOrderSum()
    },
    refresh() {},
    getOrderSum() {
        this.request({
            url: '/api/order/tongji'
        }, res => {
            this.setData({ orderSumInfo: res.data })
        })
    },
    reLogin() {
        let that = this
        wx.login({ //获取code 
            withCredentials: true,
            timeout: 10000,
            success(res) {
                let reqData = { code: res.code }
                that.request({
                    url: '/api/login',
                    login: false,
                    data: reqData
                }, ({ data }) => {
                    wx.setStorageSync('Authorization', data.access_token)
                })
            }
        })
    },
    getPhoneNumber({ detail }) {
        if (detail.errMsg === 'getPhoneNumber:ok') {
            this.request({
                url: '/api/user/audit',
                method: 'POST',
                data: { iv: detail.iv, encryptedData: detail.encryptedData }
            }, () => {
                this.getUserInfo()
            }, () => {
                this.reLogin()
                wx.$dialog.alert({
                    title: '提示',
                    message: '获取手机号码失败，请点击获取按钮重试！',
                    confirmButtonText: '我知道了',
                    theme: 'round-button',
                    beforeClose: (action) => new Promise((resolve) => {
                        resolve(true)
                    })
                })
            })
        }
    },
})